<?php
class Photos extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model(array('photo', 'komentar', 'article'));
		$this->load->helper(array('dharma_date_helper'));
		$this->userdata = $this->session->userdata('userdata');
		if($this->userdata){
			$recent_articles = $this->article->get_article(NULL, NULL, 5);
			$recent_photos = $this->photo->get_foto(NULL, NULL, 6);
		}else{
			$recent_articles = $this->article->get_article(NULL, NULL, 5, 0, 'public');
			$recent_photos = $this->photo->get_foto(NULL, NULL, 6, 0, 'public');
		}
		$this->right_sidebar = array('categories' => unserialize(ARTICLE_CATEGORY),
									 'recent_articles' => $recent_articles,
									 'recent_photos' => $recent_photos);
	}
	
	public function index(){
		if($this->userdata){
			$data['photos'] = $this->photo->get_foto();
		}else{
			$data['photos'] = $this->photo->get_foto(NULL, NULL, NULL, NULL, 'public');
		}
		$data['user'] = $this->userdata;
		$data['footer'] = $this->right_sidebar;
		$this->load->view('photo/index', $data);
	}
	
	public function detail($id){
		$photo = $this->photo->get_foto($id);
		$data = array('photo' => $photo[0],
					  'komentars' => $this->komentar->get_current_komentar('foto',$id),
					  'user' => $this->userdata);
		if($this->userdata){
			if(isset($_POST['submit_komentar'])){
				$post_data = array('jenis' => 'foto',
								   'id_item' => $id,
								   'isi' => $this->input->post('komentar'),
								   //'id_user' => $this->userdata['username'],						
								   'id_user' => $this->userdata['id_user']
								   );
				$this->komentar->add($post_data);
				redirect(site_url('/photos/detail/'.$id));
			}
		}
		$data['right_sidebar'] = $this->right_sidebar;
		$this->load->view('photo/detail',$data);
	}
	
	public function user_photos(){
		$data['photos'] = $this->photo->get_foto(NULL, $this->userdata['id_user']);
		$data['footer'] = $this->right_sidebar;
		$this->load->view('photo/user_photo', $data);
	}
	
	public function add(){
		if(isset($_POST['photo_submit'])){
			$random           = rand(000000,999999);
			$post_data = array('id_user' => $this->userdata['id_user'],
							   'album' => $this->input->post('album'),
							   'judul' => $this->input->post('judul'),
							   'deskripsi' => $this->input->post('deskripsi'),
							   'status' => $this->input->post('status'),
							   'foto' => $random."_".strtolower($_FILES['photo_upload']['name'])
							   );
			$id = $this->photo->add($post_data);
			
			//upload
			$config['upload_path'] = './images/photos/';
			$config['allowed_types'] = 'jpg|png|gif|jpeg';
			$config['file_name'] = $random."_".$_FILES['photo_upload']['name'];
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('photo_upload', TRUE)) { // upload image to server
				$error = $this->upload->display_errors();
				?><script type="text/javascript"> alert('<?php echo $error; ?>'); </script><?php
				$this->photo->delete($id);
			} else {
				redirect(site_url('/photos/user_photos'));
			}
		}
		$data['photos'] = NULL;
		$data['footer'] = $this->right_sidebar;
		$this->load->view('photo/form', $data);
	}
	
	public function edit($id){
		$photos = $this->photo->get_foto($id);
		if(isset($_POST['photo_submit'])){
			//edit content image
			$image_upload = $_FILES['photo_upload']['name'];
			if ($image_upload != null) {
				$random           			= rand(000000,999999);
				$config['upload_path'] 		= './images/photos/';
				$config['allowed_types'] 	= 'jpg|png|gif|jpeg';
				$url 						= './images/photos/'.$photos[0]['foto'];
				if (isset($url)) {
					if($remove = unlink($url)){
						$config['file_name'] = $random."_".$image_upload;
						$this->load->library('upload', $config);
						if (!$this->upload->do_upload('photo_upload', TRUE)) { // upload image to server
							$error = $this->upload->display_errors();
							echo $error;
						} 
					}else{
						echo "not removed"; die;
					}
				}
				$post_data = array('album' => $this->input->post('album'),
								   'judul' => $this->input->post('judul'),
								   'deskripsi' => $this->input->post('deskripsi'),
								   'status' => $this->input->post('status'),
								   'foto' => $random."_".strtolower($_FILES['photo_upload']['name'])
								   );
				$this->photo->edit($id, $post_data);
			}else{
				$post_data = array('album' => $this->input->post('album'),
								   'judul' => $this->input->post('judul'),
								   'deskripsi' => $this->input->post('deskripsi'),
								   'status' => $this->input->post('status')
								   );
				$this->photo->edit($id, $post_data);				  
			}
			redirect(site_url('photos/user_photos'));
		}
		$data['photos'] = $photos;
		$data['footer'] = $this->right_sidebar;
		$this->load->view('photo/form', $data);
	}
	
	public function delete($id){
		$photo = $this->photo->get_foto($id);
		$this->photo->delete($id);
		$this->komentar->delete($id);
		unlink('./images/photos/'.$photo[0]['foto']);
		redirect('photos/user_photos');
	}
}
?>